unit UFrmNewDBCon;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFrmBaseDialog, StdCtrls, Buttons, ExtCtrls, Registry, Spin;

type
  TFrmNewDBCon = class(TFrmBaseDialog)
    Label3: TLabel;
    cbbDBType: TComboBox;
    edtHost: TEdit;
    edtUserID: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    edtPassword: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    edtPort: TSpinEdit;
    edtDBName: TComboBox;
    Label2: TLabel;
    procedure FormShow(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure cbbDBTypeClick(Sender: TObject);
    procedure edtDBNameDropDown(Sender: TObject);
  private
    function isEnablePGSQL: Boolean;
  public
    ConStr: string;
  end;

implementation

{$R *.dfm}

{ TFrmNewDBCon }

procedure TFrmNewDBCon.btn1Click(Sender: TObject);
const
  MSSQL = 'Provider=MSSQL;DataSource=%s;InitialCatalog=%s;UserID=%s;Password=%s;Persist SecurityInfo=True;Port=%d';
  PGSQL = 'Provider=PGSQLODBC.1;DataSource=%s;InitialCatalog=%s;UserID=%s;Password=%s;Persist SecurityInfo=True;Port=%d';
var
  s, Password, UserName, Host, Database: string;
begin
  inherited;
  Password := trim(edtPassword.Text);
  Database := trim(edtDBName.Text);
  Host := Trim(edtHost.Text);
  UserName := trim(edtUserID.Text);
  if cbbDBType.Items[cbbDBType.ItemIndex] = 'MSSQL' then
  begin
    ConStr := Format(MSSQL, [Host, Database, UserName, Password,edtPort.Value]);
  end;
  if cbbDBType.Items[cbbDBType.ItemIndex] = 'PGSQLODBC' then
  begin
    ConStr := Format(PGSQL, [Host, Database, UserName, Password,edtPort.Value]);
  end;
  ModalResult := mrOk;
end;

procedure TFrmNewDBCon.cbbDBTypeClick(Sender: TObject);
begin
  inherited;
  if cbbDBType.Items[cbbDBType.ItemIndex] = 'MSSQL' then
    edtPort.Value := 1433;
  if cbbDBType.Items[cbbDBType.ItemIndex] = 'PGSQLODBC' then
    edtPort.Value := 5432;
end;

procedure TFrmNewDBCon.edtDBNameDropDown(Sender: TObject);
begin
  inherited;
//
end;

procedure TFrmNewDBCon.FormShow(Sender: TObject);
begin
  inherited;
  if isEnablePGSQL then
    cbbDBType.Items.Add('PGSQLODBC');
end;

function TFrmNewDBCon.isEnablePGSQL: Boolean;
var
  aReg: TRegistry;
begin
  Result := False;
  aReg := TRegistry.Create;
  try
    aReg.RootKey := HKEY_LOCAL_MACHINE;
    areg.OpenKey('SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers', False);
    Result := UpperCase(aReg.ReadString('PostgreSQL ANSI')) = 'INSTALLED';
  finally
    aReg.Free;
  end;
end;

end.

